#include "predictorg.h"

void init_predictor ()
{
    GR = 0;
    for (int i = 0; i < HIST_TABLE_SIZE; i++)
        history_table[i] = 0;
}

bool make_prediction (unsigned int pc)
{
    unsigned index = (pc ^ GR) & (HIST_TABLE_SIZE - 1);
    return history_table[index] >= 2;
}

void train_predictor (unsigned int pc, bool outcome)
{
    unsigned index = (pc ^ GR) & (HIST_TABLE_SIZE - 1);

    /* Shift global register */
    GR = GR << 1;
    if (outcome)
    {
        GR++;
        if (history_table[index] < 3)
            history_table[index]++;
    }
    else
    {
        if (history_table[index] > 0)
            history_table[index]--;
    }
}
